package exam.test3;

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * name: ***
 */
public class VolleyBallTeam {
	private String name;
	private ArrayList<String> gameResults;
	private double gamesPoint;
	private int totalGameNumber;

	public VolleyBallTeam(String name, double gamesPoint, int totalGameNumber) {
		if (name == null || name.trim().isEmpty()) {
			this.name = "unknow";
		} else {
			this.name = name;
		}

		if (gamesPoint < 0) {
			this.gamesPoint = 0;
		} else {
			this.gamesPoint = gamesPoint;
		}

		if (totalGameNumber < 0) {
			this.totalGameNumber = 0;
		} else {
			this.totalGameNumber = totalGameNumber;
		}

		this.gameResults = new ArrayList<>();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		if (name == null || name.trim().isEmpty()) {
			this.name = "unknow";
		} else {
			this.name = name;
		}
	}

	public void readGameResultFile() {
		try {
			FileReader fileReader = new FileReader("teamResults.txt");
			Scanner scanner = new Scanner(fileReader);
			while (scanner.hasNextLine()) {
				String line = scanner.nextLine().trim();
				if (line.equals("win") || line.equals("loss") || line.equals("draw"))
					gameResults.add(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void readGameResult() {
		for (int i = 0; i < gameResults.size(); i++) {
			String result = gameResults.get(i);
			if (result.equals("win")) {
				gamesPoint += 1;
			} else if (result.equals("loss")) {
				gamesPoint += 0.5d;
			}
			totalGameNumber++;
		}
	}

	public void display() {
		if (totalGameNumber == 0) {
			System.out.println("no games played in this season");
		} else {
			double averagePoints = gamesPoint / totalGameNumber * 100;
			if (averagePoints > 50) {
				System.out.println("winning season");
			} else {
				System.out.println("losing season");
			}
		}
	}
}
